/*
 * Unit tests for the Loan class
 * @author M.T. Segarra, M.A. Taupenot
 * @version 1.0.0
 * 25/05/2012
 */
package tests;

import static org.junit.Assert.assertTrue;
import org.junit.*;
import java.util.*;

import loansManagement.Loan;
import subscribersManagement.Subscriber;
import booksManagement.Book;
import booksManagement.DVD;
import booksManagement.Copy;
import exceptions.*;

/**
 * @author        user
 */
public class LoanTest {

	/**
   * @uml.property  name="l"
   * @uml.associationEnd  
   */
	private Loan l;
	/**
   * @uml.property  name="b"
   * @uml.associationEnd  
   */
	private Book b;
	/**
   * @uml.property  name="d"
   * @uml.associationEnd  
   */
	private DVD d;
	/**
   * @uml.property  name="c1"
   * @uml.associationEnd  
   */
	private Copy c1;
	/**
   * @uml.property  name="c2"
   * @uml.associationEnd  
   */
	private Copy c2;
	/**
   * @uml.property  name="s"
   * @uml.associationEnd  
   */
	private Subscriber s;

	@Before
	public void beforeTests() throws BadParametersException {
		this.s = new Subscriber("Mayte", "Segarra", new GregorianCalendar());
		ArrayList<String> authors = new ArrayList<String>();
		this.b = new Book("Noche", authors, new GregorianCalendar(), "026765415");
		this.d = new DVD("Nemesis", new GregorianCalendar(),"15348434");
		c1 = b.addCopy();
		c2 = d.addCopy();
	}

	@Test
	public void testEquals() throws BadParametersException, TooManyLoansException, LentBookException, ReservedCopyException {
		
		ArrayList<String> authors = new ArrayList<String>();
		Book b2 = new Book("Noche", authors, new GregorianCalendar(), "026765417");
		Copy c12 = b2.addCopy();
		
		// The same subscriber loans two books
		l = new Loan(c1, s);	
		Loan l2 = new Loan(c12, s);
		assertTrue(!l.equals(l2));
		
		// Different subscribers loan different books
		authors = new ArrayList<String>();
		Book b3 = new Book("Noche", authors, new GregorianCalendar(), "026765418");
		Copy c13= b3.addCopy();
		this.s = new Subscriber("M", "Segarra", new GregorianCalendar());
		Loan l3 = new Loan(c13, s);
		assertTrue(!l.equals(l3));
		
		// Book unit test tests if loaning two copies of the same book
		Copy c4 = b.addCopy();
		Loan l4 = new Loan(c4,s);
		assertTrue(!l.equals(l4));
		//Check if ot is indeed the same book
		assertTrue(l.getCopy().getMedia().equals(l4.getCopy().getMedia()));
	}
	
	@Test
	public void testLoan() throws BadParametersException, TooManyLoansException, LentBookException, ReservedCopyException {
		l = new Loan(c1, s);
		assertTrue(l.getCopy().equals(c1) && l.getLender().equals(s));
		assertTrue(s.getCurrentLoans().size() == 1);
		assertTrue(c1.isLent());
	}

	@Test(expected = BadParametersException.class)
	public void testNullLoan() throws BadParametersException, TooManyLoansException,
			LentBookException, ReservedCopyException {
		new Loan(null, null);
		/* new Loan(null, new Subscriber("Mayte", "Segarra", new GregorianCalendar()));
		 * ArrayList<String> authors = new ArrayList<String>();
		 * authors.add("Corral");
		 * new Loan(new Book("La nuit", authors, new GregorianCalendar(), "026765415"), null);
		 */
	}

	@Test
	public void testReturnBook() throws BadParametersException, TooManyLoansException,
			LentBookException, ReservedCopyException {
		l = new Loan(c1, s);
		l.returnCopy();
		assertTrue(l.getCopy() == null);
		assertTrue(l.getLender() == null);
		assertTrue(l.getReturnDate() == null);
	}

}
